package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.operate.domain.OSeedMng;
import com.sgcc.operate.service.IOSeedMngService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OGroupMng;
import com.sgcc.operate.service.IOGroupMngService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 群组管理Controller
 * 
 * @author sgcc
 * @date 2021-03-29
 */
@RestController
@RequestMapping("/groupmng")
public class OGroupMngController extends BaseController
{
    @Autowired
    private IOGroupMngService oGroupMngService;

    /**
     * 查询群组管理列表
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:list")
    @GetMapping("/list")
    public TableDataInfo list(OGroupMng oGroupMng)
    {
        startPage();
        List<OGroupMng> list = oGroupMngService.selectOGroupMngList(oGroupMng);
        return getDataTable(list);
    }

    /**
     * 导出群组管理列表
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:export")
    @Log(title = "群组管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OGroupMng oGroupMng) throws IOException
    {
        List<OGroupMng> list = oGroupMngService.selectOGroupMngList(oGroupMng);
        ExcelUtil<OGroupMng> util = new ExcelUtil<OGroupMng>(OGroupMng.class);
        util.exportExcel(response, list, "groupmng");
    }

    /**
     * 获取群组管理详细信息
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(oGroupMngService.selectOGroupMngById(id));
    }


    /**
     * 新增群组管理
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:add")
    @Log(title = "群组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OGroupMng oGroupMng)
    {
        return toAjax(oGroupMngService.insertOGroupMng(oGroupMng));
    }

    /**
     * 修改群组管理
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:edit")
    @Log(title = "群组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OGroupMng oGroupMng)
    {
        return toAjax(oGroupMngService.updateOGroupMng(oGroupMng));
    }

    /**
     * 删除群组管理
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:remove")
    @Log(title = "群组管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oGroupMngService.deleteOGroupMngByIds(ids));
    }


    /**
     * 查询群组管理列表
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:list")
    @GetMapping("/optionselect")
    public AjaxResult optionselect(OGroupMng oGroupMng)
    {
        return AjaxResult.success(oGroupMngService.selectOGroupMngList(oGroupMng));
    }

    @Autowired
    private IOSeedMngService oSeedMngService;

    /**
     * 获取群组管理详细信息
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:query")
    @GetMapping(value = "judgeQuoted/{ids}")
    public AjaxResult judgeQuoted(@PathVariable("ids") String[] ids)
    {

        for (int id = 0; id < ids.length; id++) {
            OSeedMng seedMng= new OSeedMng();
            seedMng.setGroupid(ids[id]);
            List<OSeedMng> oSeedMngs = oSeedMngService.selectOSeedMngList(seedMng);
            if(oSeedMngs.size()>0){
                return AjaxResult.error("选中的群组已被引用无法删除");
            }
        }
        return AjaxResult.success();
    }
}
